package com.torry.edss.domain.accessor.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.torry.edss.domain.valueobject.Bourse;
import com.torry.edss.domain.valueobject.MarketPlate;
import com.torry.edss.infras.entity.CompanyDO;
import com.torry.edss.domain.model.Company;
import com.torry.edss.infras.mapper.CompanyMapper;
import com.torry.edss.domain.accessor.CompanyAccessor;
import org.springframework.stereotype.Service;
import com.torry.boot.starter.mybatisplus.service.AccessorImpl;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author torry.wang
 * @since 2025-05-07
 */
@Service
@SuppressWarnings("DuplicatedCode")
public class CompanyAccessorImpl extends AccessorImpl<CompanyMapper, CompanyDO, Company> implements CompanyAccessor {
    @Override
    public Company toModel(CompanyDO entity) {
        if (entity == null) return null;
        Company company = new Company();
        company.setName(entity.getName());
        company.setCode(entity.getCode());
        company.setBourse(Bourse.parse(entity.getBourse()));
        company.setPlate(MarketPlate.parse(entity.getPlate()));
        company.setAcronym(entity.getAcronym());
        company.setLdate(entity.getLdate());
        company.setMarketValue(entity.getMarketValue());
        company.setDelisted(entity.getDelisted() == 1);
        company.setId(entity.getId());
        company.setCreateTime(entity.getCreateTime());
        company.setUpdateTime(entity.getUpdateTime());
        return company;
    }

    @Override
    public CompanyDO toEntity(Company model) {
        if (model == null) return null;

        CompanyDO companyDO = new CompanyDO();
        companyDO.setName(model.getName());
        companyDO.setCode(model.getCode());
        companyDO.setBourse(model.getBourse().getCode());
        companyDO.setPlate(model.getPlate().getCode());
        companyDO.setAcronym(model.getAcronym());
        companyDO.setLdate(model.getLdate());
        companyDO.setMarketValue(model.getMarketValue());
        companyDO.setDelisted(model.getDelisted() ? 1 : 0);
        companyDO.setId(model.getId());
        companyDO.setCreateTime(model.getCreateTime());
        companyDO.setUpdateTime(model.getUpdateTime());
        return companyDO;
    }

    @Override
    public Company getByCode(String code) {
        return this.getOne(new LambdaQueryWrapper<CompanyDO>().eq(CompanyDO::getCode, code));
    }

    @Override
    public String getCode(Long companyId) {
        return this.getById(companyId).getCode();
    }
}
